package com.quartz.monitor4job.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Properties;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author LIPENG
 *
 * @date 2017年7月11日 下午8:43:10
 *
 * @version V1.0
 */
public class BaseController {
	private static final VelocityEngine ve = new VelocityEngine(); 
	private static final Logger logger = LoggerFactory.getLogger(QuartzController.class);
	
	static{
		// 初始化Velocity引擎  
        InputStream is = BaseController.class.getResourceAsStream("/velocity.properties");  
        Properties properties = new Properties();  
        try {  
            properties.load(is);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        ve.init(properties);  
	}
	public String generateHtml(String vmTemplate,Object obj){
		StringWriter sw = new StringWriter();
		try {
			VelocityContext ctx = new VelocityContext();
			ctx.put("data", obj);
			ve.getTemplate(vmTemplate,"UTF-8").merge(ctx, sw);
		} catch (Exception e) {
			logger.error("转化vm模板出错",e);
		}finally{
			if(sw!=null){
				try {
					sw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return sw.toString();
		
	}
}
